<template>
    <div>
        <HomePageHead></HomePageHead>
        <HomePageNavigation></HomePageNavigation>
        <HomeSecondaryHeading :titleData="pageData" :pinyinid="routeId"></HomeSecondaryHeading>
        <main class="index_main">
            <section class="index_1 clearfix">
                <div class="index_1_left">
                    <div class="roll_1_box">
                        <HomeSmallSwiper1 :data="pageData[0].data"></HomeSmallSwiper1>
                    </div>
                </div>
                <div class="index_1_right">
                    <div class="index_1_color_box">
                        <h3 class="index_1_head_h3 clearfix">
                            <NuxtLink class="index_1_head_a" :href="getLinkPath(pageData[0])" v-if="pageData[0]"
                                :title="pageData[0].alias">
                                {{ pageData[0].alias }}
                            </NuxtLink>
                        </h3>
                        <div class="index_1_ul" v-if="pageData[0].data2">
                            <NuxtLink class="index_1_ul_a dot1" :href="getLinkPathDetail(item)" :title="item.title"
                                v-for="item in pageData[0].data2" :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'">
                                {{ item.title }}
                            </NuxtLink>
                        </div>
                    </div>
                </div>
            </section>
            <HomeAdvertisingClass :adData1="adImg1" :adData2="adImg2"></HomeAdvertisingClass>
            <section class="index_2 clearfix">
                <div class="index_2_left" v-if="pageData[1]">
                    <h3 class="index_2_left_h3 clearfix">
                        <NuxtLink class="index_2_left_h3_a index_2_left_h3_a_only" :href="getLinkPath(pageData[1])"
                            v-if="pageData[1]" :title="pageData[1].alias">
                            {{ pageData[1].alias }}
                        </NuxtLink>
                    </h3>
                    <div class="index_2_left_img_out">
                        <div class="index_2_big_img_ul clearfix">
                            <div class="index_2_big_img_li">
                                <NuxtLink class="index_2_big_img_li_a" :href="getLinkPathDetail(item)"
                                    :title="item.title" v-for="item in pageData[1].data" :key="item.id"
                                    :target="item.islink == 1 ? '_blank' : '_self'">
                                    <img :src="item.imgurl" title="item.title" alt="item.title">
                                    <div class="index_2_img_li_dot1 dot1">{{ item.title }}</div>
                                </NuxtLink>
                            </div>
                        </div>
                        <div class="index_2_img_ul clearfix">
                            <div class="index_2_img_li" v-for="item in pageData[1].data2" :key="item.id">
                                <NuxtLink class="index_2_img_li_a" :href="getLinkPathDetail(item)" :title="item.title"
                                    :target="item.islink == 1 ? '_blank' : '_self'">
                                    <img :src="item.imgurl" title="item.title" alt="item.title">
                                    <div class="index_2_img_li_dot1 dot1">{{ item.title }}</div>
                                </NuxtLink>
                            </div>
                        </div>
                    </div>

                    <div class="index_2_left_ul_box">
                        <div class="index_2_left_ul index_2_left_ul_group_6">
                            <div class="index_2_left_ul_li dot1" v-for="item in pageData[1].data3" :key="item.id">
                                <NuxtLink class="index_2_left_ul_li_a_name" :href="getLinkPathDetail(item)"
                                    :title="item.title" :key="item.id" :target="item.islink == 1 ? '_blank' : '_self'">
                                    资讯关注
                                </NuxtLink>
                                <NuxtLink class="index_2_left_ul_li_a_text" :href="getLinkPathDetail(item)"
                                    :title="item.title" :key="item.id" :target="item.islink == 1 ? '_blank' : '_self'">
                                    {{ item.title }}
                                </NuxtLink>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="index_2_right" v-if="pageData[2]">
                    <h3 class="index_2_right_head">
                        <NuxtLink class="index_2_right_head_a" :href="getLinkPath(pageData[2])" v-if="pageData[2]"
                            :title="pageData[2].alias">
                            {{ pageData[2].alias }}
                        </NuxtLink>
                    </h3>
                    <div class="index_2_right_img_ul">
                        <div class="index_2_right_img_li clearfix" v-for="item in pageData[2].data" :key="item.id">
                            <NuxtLink class="index_2_right_img_li_a" :href="getLinkPathDetail(item)" :title="item.title"
                                :key="item.id" :target="item.islink == 1 ? '_blank' : '_self'">
                                <div class="index_2_right_img_li_a_img_box">
                                    <img class="" :src="item.imgurl" title="" alt="">
                                </div>
                                <div class="index_2_right_img_li_dot3 dot3">
                                    {{ item.title }}
                                </div>
                            </NuxtLink>
                        </div>
                    </div>
                    <div class="index_1_ul index_1_ul_2">
                        <NuxtLink class="index_1_ul_a dot1" :href="getLinkPathDetail(item)" :title="item.title"
                            v-for="item in pageData[2].data3" :key="item.id"
                            :target="item.islink == 1 ? '_blank' : '_self'">
                            {{ item.title }}
                        </NuxtLink>
                    </div>
                    <div class="index_2_right_img_ul">
                        <div class="index_2_right_img_li clearfix" v-for="item in pageData[2].data2" :key="item.id">
                            <NuxtLink class="index_2_right_img_li_a" :href="getLinkPathDetail(item)" :title="item.title"
                                :key="item.id" :target="item.islink == 1 ? '_blank' : '_self'">
                                <div class="index_2_right_img_li_a_img_box">
                                    <img class="" :src="item.imgurl" title="" alt="">
                                </div>
                                <div class="index_2_right_img_li_dot3 dot3">
                                    {{ item.title }}
                                </div>
                            </NuxtLink>
                        </div>
                    </div>
                </div>
            </section>

            <section class="index_3 clearfix">
                <div class="index_2_left" v-if="pageData[4]">
                    <h3 class="index_2_left_h3 clearfix">
                        <NuxtLink :class="{ 'index_2_left_h3_a': true, 'index_2_left_h3_a_only': showTabs == 1 }"
                            :href="getLinkPath(pageData[3])" v-if="pageData[3]" :title="pageData[3].alias"
                            @mouseenter="qhtabs(1)">
                            {{ pageData[3].alias }}
                        </NuxtLink>
                        <NuxtLink :class="{ 'index_2_left_h3_a': true, 'index_2_left_h3_a_only': showTabs == 2 }"
                            :href="getLinkPath(pageData[4])" v-if="pageData[4]" :title="pageData[4].alias"
                            @mouseenter="qhtabs(2)">
                            {{ pageData[4].alias }}
                        </NuxtLink>
                    </h3>
                    <div v-if="showTabs == 1">
                        <div class="index_2_left_img_out">
                            <div class="index_2_left_ul index_2_left_ul_group_5">
                                <div class="index_2_left_ul_li dot1" v-for="item in pageData[3].data" :key="item.id">
                                    <NuxtLink class="index_2_left_ul_li_a_name" :href="getLinkPathDetail(item)"
                                        :title="item.title" :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'">
                                        资讯关注
                                    </NuxtLink>
                                    <NuxtLink class="index_2_left_ul_li_a_text" :href="getLinkPathDetail(item)"
                                        :title="item.title" :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'">
                                        {{ item.title }}
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                        <div class="index_2_left_ul_box">
                            <div class="index_3_img_ul">
                                <div class="index_3_img_li clearfix" v-for="item in pageData[3].data2" :key="item.id">
                                    <NuxtLink class="index_3_img_li_a" :href="getLinkPathDetail(item)"
                                        :title="item.title" :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'">
                                        <div class="index_3_img_li_img_box">
                                            <img :src="item.imgurl" title="item.title" alt="item.title">
                                        </div>
                                        <div class="index_3_img_li_in">
                                            <div class="index_3_img_li_dot3 dot3">
                                                {{ item.title }}
                                            </div>
                                            <span class="index_3_img_li_tag">资讯关注</span>
                                        </div>
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div v-if="showTabs == 2">
                        <div class="index_2_left_img_out">
                            <div class="index_2_left_ul index_2_left_ul_group_5">
                                <div class="index_2_left_ul_li dot1" v-for="item in pageData[4].data" :key="item.id">
                                    <NuxtLink class="index_2_left_ul_li_a_name" :href="getLinkPathDetail(item)"
                                        :title="item.title" :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'">
                                        资讯关注
                                    </NuxtLink>
                                    <NuxtLink class="index_2_left_ul_li_a_text" :href="getLinkPathDetail(item)"
                                        :title="item.title" :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'">
                                        {{ item.title }}
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                        <div class="index_2_left_ul_box">
                            <div class="index_3_img_ul">
                                <div class="index_3_img_li clearfix" v-for="item in pageData[4].data2" :key="item.id">
                                    <NuxtLink class="index_3_img_li_a" :href="getLinkPathDetail(item)"
                                        :title="item.title" :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'">
                                        <div class="index_3_img_li_img_box">
                                            <img :src="item.imgurl" title="item.title" alt="item.title">
                                        </div>
                                        <div class="index_3_img_li_in">
                                            <div class="index_3_img_li_dot3 dot3">
                                                {{ item.title }}
                                            </div>
                                            <span class="index_3_img_li_tag">资讯关注</span>
                                        </div>
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="index_2_right">
                    <h3 class="index_2_right_head">
                        <NuxtLink class="index_2_right_head_a" :href="getLinkPath(pageData[5])" v-if="pageData[5]"
                            :title="pageData[5].alias">
                            {{ pageData[5].alias }}
                        </NuxtLink>
                    </h3>
                    <div class="index_3_num_ul" v-if="pageData[5]">
                        <NuxtLink class="index_1_ul_a dot1" :href="getLinkPathDetail(item)" :title="item.title"
                            v-for="item in pageData[5].data" :key="item.id"
                            :target="item.islink == 1 ? '_blank' : '_self'">
                            {{ item.title }}
                        </NuxtLink>
                    </div>
                </div>
            </section>
        </main>
        <!-- 页脚部分 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//0.加载页面依赖 start ---------------------------------------->
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
//0.加载页面依赖 end ---------------------------------------->

//1.获得路由id start ---------------------------------------->
const targetSegment = getRoutePath(1);
//1.1 获得当前的路由id
let routeId;
//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    routeId = getRouteId.data.category_id
} else {
    console.log("获得路由id出错！", getRouteId.message)
}
//1.获得路由id end ---------------------------------------->

//2.选项卡 start ---------------------------------------->
let showTabs = ref(1)
let qhtabs = function (number) {
    console.log(number)
    showTabs.value = number
}
//2.选项卡 end ---------------------------------------->

//3.广告 start ---------------------------------------->
let adImg1 = ref({})
let adImg2 = ref({})
onMounted(async () => {
    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmw_category_0001`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg1.value = resultAd1.data[0];

    //广告2
    let url2 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmw_category_0002`
    const responseAd2 = await fetch(url2, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd2 = await responseAd2.json();
    adImg2.value = resultAd2.data[0];
})
//3.广告 end ---------------------------------------->

//4.页面数据 start ---------------------------------------->
const pageData = ref([
    //示例：
    // {
    //     id: 1, 
    //     title: "", //该导航的标题
    //     data: [], //数据1
    //     data1: [], //数据2
    //     data2: [], //数据3
    //     data3: [], //数据4
    //     data4: [], //数据5
    //     category_id1: "",  //子导航id1
    //     category_id2: "", 
    //     category_id3: "",
    //     category_id4: "",
    //     title1: "", //子导航标题1
    //     title2: "",
    //     title3: "",
    //     title4: "",
    //     cid: "" //该导航的id
    // },
])

//4.1 获取所有导航
try {
    const navigateData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': routeId,
            'num': 16
        },
    });
    if (navigateData.code == 200) {
        // 遍历可用的导航池放到页面中
        for (let index in navigateData.data) {
            let data = {
                title: navigateData.data[index].name,
                cid: navigateData.data[index].category_id,
                children_count: navigateData.data[index].children_count,
                alias: navigateData.data[index].alias,
                aLIas_pinyin: targetSegment + "/" + navigateData.data[index].aLIas_pinyin,
                data: [],
                data1: [],
                data2: [],
                data3: [],
                data4: [],
                category_id1: "",
                category_id2: "",
                category_id3: "",
                category_id4: "",
                title1: "",
                title2: "",
                title3: "",
                title4: ""
            };
            if (navigateData.data[index].is_url == 1) {
                // 处理 URL 的逻辑
            } else {
                //每个页面最多8个模块
                pageData.value.push(data);
            }
        }
        //导航池加载完毕，开始申请模块数据
        let getJson = [
            // { "parent": routeId + ",0,0", "child": pageData.value[0].cid + ",5,7" },//模块1理论前沿
            // { "parent": routeId + ",0,0", "child": pageData.value[1].cid + ",3,11" },//模块2典型经验
            // { "parent": routeId + ",0,0", "child": pageData.value[2].cid + ",4,2" },//模块3农业天地
            // { "parent": routeId + ",0,0", "child": pageData.value[3].cid + ",3,10" },//模块4美丽乡村
            // { "parent": routeId + ",0,0", "child": pageData.value[4].cid + ",3,10" },//模块5农民之家
            // { "parent": routeId + ",0,0", "child": pageData.value[5].cid + ",0,7" },//模块6农业天地
            // { "parent": routeId + ",0,0", "child": pageData.value[6].cid + ",2,3" },//模块7农村建设
            // { "parent": routeId + ",0,0", "child": pageData.value[7].cid + ",4,0" },//模块8高端资讯
        ]
        for (let index in pageData.value) {
            // console.log("index", index)
            if (index == 0) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",5,7" })//模块1理论前沿
            } else if (index == 1) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",3,11" })//模块2典型经验
            } else if (index == 2) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",4,2" })//模块3农业天地
            } else if (index == 3) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",3,10" })//模块4美丽乡村
            } else if (index == 4) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",3,10" })//模块5农民之家 
            } else if (index == 5) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",0,7" })//模块6农业天地 
            } else if (index == 6) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",2,3" })//模块7农村建设 
            } else if (index == 7) {
                getJson.push({ "parent": routeId + ",0,0", "child": pageData.value[index].cid + ",4,0" })//模块8高端资讯 
            }
        }
        let jsonString = JSON.stringify(getJson);
        getPageAllData(jsonString);

    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：分类页导航池")
        console.log("后端错误反馈：", navigateData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
} catch (error) {
    console.log("错误：导航池渲染执行接口出错！请检查频道页渲染的模块数据是否完整！")
}


async function getPageAllData(jsonString) {
    const mkdata = await requestDataPromise('/web/getWebsiteAllArticle', {
        method: 'GET',
        query: {
            'id': jsonString
        },
    });
    console.log('模块数据：', mkdata);


    if (mkdata.code == 200) {
        //模块1
        pageData.value[0].data = mkdata.data[0].child.imgnum;
        pageData.value[0].data2 = mkdata.data[0].child.textnum;
        //模块2
        for (let index in mkdata.data[1].child.imgnum) {
            if (index == 0) {
                pageData.value[1].data.push(mkdata.data[1].child.imgnum[index]);
            } else {
                pageData.value[1].data2.push(mkdata.data[1].child.imgnum[index]);
            }
        }
        pageData.value[1].data3 = mkdata.data[1].child.textnum;
        //模块3
        for (let index in mkdata.data[2].child.imgnum) {
            if (index <= 1) {
                pageData.value[2].data.push(mkdata.data[2].child.imgnum[index]);
            } else {
                pageData.value[2].data2.push(mkdata.data[2].child.imgnum[index]);
            }
        }
        pageData.value[2].data3 = mkdata.data[2].child.textnum;
        //模块4
        pageData.value[3].data = mkdata.data[3].child.textnum;
        pageData.value[3].data2 = mkdata.data[3].child.imgnum;
        //模块5
        pageData.value[4].data = mkdata.data[4].child.textnum;
        pageData.value[4].data2 = mkdata.data[4].child.imgnum;
        //模块6
        pageData.value[5].data = mkdata.data[5].child.textnum;

    } else {
        ElMessage.error(mkdata.message)
    }
}
//4.页面数据 end ---------------------------------------->

//5.设置seo信息 start---------------------------------------->
const setData = await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': routeId
    },
});

if (setData.code == 200) {
    let seoTitle = setData.data.seo_title;
    let seoDescription = setData.data.seo_description;
    let seoKeywords = setData.data.seo_keywords;
    let seoSuffix = setData.data.suffix;
    let seoName = setData.data.website_name;

    useSeoMeta({
        title: seoTitle + "_" + seoSuffix,
        meta: [
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 }
        ]
    });
} else {
    console.log("设置频道页SEO出错！", setData.message)
}
//5.设置seo信息 end---------------------------------------->
</script>

<style lang="less" scoped>
@import '@/assets/css/class.less';
</style>